<template>
  <div class="chat-item">
    <template v-if="item.type<9">
      <div>
        <div v-if="show" class="time"><span>{{ formatSimpleDateTime(item.createTime) }}</span></div>
        <div class="chat-body" :class="item.from!==onlineId?'chat-left':'chat-right'">
          <div class="head">
            <app-avatar :size="50" :src="item.avatar"/>
            <span v-if="item.from!==onlineId &&item.from.startsWith('customer_')">
              <span >
                [客服]
              </span>
              {{item.nickName}}
            </span>
          </div>
          <div class="box" :class="item.from!==onlineId?'left':'right'"/>
          <div class="msg">
            <msg-text v-if="item.type===0" :message="item.message"></msg-text>
            <img
                class="image"
                v-else-if="item.type===1"
                @load="imgLoad"
                :src="item.message"
            >
            <div v-else-if="item.type===2">
              <item-video @imgLoad="videoImgLoad" :item="item"></item-video>
            </div>
            <!--语音-->
            <msg-audio v-else-if="item.type===3" :message="item.message"></msg-audio>
          </div>
          <div class="readType" v-if="item.from===onlineId">
            <text v-if="item.status===1" style="color: #8f949f">已读</text>
            <text v-else>未读</text>
          </div>
        </div>
      </div>
    </template>
    <template v-else>
      <div class="sys-info">
        {{ item.message }}
      </div>
    </template>
  </div>
</template>

<script lang="ts" setup>
import {IChatMessage} from "@/api/types/chat";
import {onMounted, PropType, ref} from "vue";
import ItemVideo from './itemVideo.vue'
import {formatSimpleDateTime} from "@/utils/constants";
import {store} from "@/store";
import MsgAudio from "@/components/Monitor/components/msg-audio.vue";
import MsgText from "@/components/Monitor/components/msg-text.vue";

const props = defineProps({
  item: {
    type: Object as PropType<IChatMessage | null>,
    default: () => {
    }
  },
  onlineId: {
    type: String,
    default: '03022308-igrzh'
  }
});

interface IEmitsType {
  (e: 'imgLoad', num: number): void;
}

const show = ref(false)
onMounted(() => {
  const time = props.item?.createTime! - store.state.customer.chatTime!
  if (time > 60) {
    show.value = true
    store.state.customer.chatTime = props.item?.createTime!
  }
})

const emit = defineEmits<IEmitsType>();

const videoImgLoad = (e: number) => {
  emit('imgLoad', e)
}

const imgLoad = (e: any) => {
  const img = e.path[0] as HTMLImageElement
  emit('imgLoad', img.offsetTop + img.offsetHeight)
}

</script>

<style lang="scss" scoped>

$leftColor: #0A98D5;
$rightColor: #c3d0d5;

div.chat-item {
  min-height: 10px;
  display: flex;
  flex-direction: column;

  div.sys-info {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 18px;
    margin-top: 10px;
    color: #999999;
  }

  // 历史颜色
  div.hist {
    background: #ebeee4;
  }

  // 时间
  ::v-deep div.time {
    width: 100%;
    text-align: center;
    color: #FFFFFF;
    margin: 5px 0px;

    span {
      padding: 0px 5px;
      background: #999999;
      border-radius: 5px;
      line-height: 14px;
    }
  }

  div.chat-body {
    display: flex;
    justify-content: flex-start;
    padding-bottom: 5px;

    .head {
      position: relative;
      width: 60px;
      color: #1f2225;
      margin-left: -10px;
      margin-right: -10px;
      display: flex;
      justify-content: flex-start;
      align-items: center;
      flex-direction: column;

      &>span{
        font-size: 11px;
        color: #8c939d;
        word-wrap: break-word; /*强制换行*/
        overflow: hidden; /*超出隐藏*/
        text-overflow: ellipsis; /*隐藏后添加省略号*/
        white-space: nowrap; /*强制不换行*/
        width: 60px;
        text-align: center;

        &>span{
          color: #4d42e3;
        }
      }
    }

    .box {
      width: 0px;
      height: 0px;
      border-width: 10px;
      border-style: solid;
      margin-top: 15px;
      &.left {
        border-top-color: transparent;
        border-bottom-color: transparent;
        border-left-color: transparent;
      }

      &.right {
        border-top-color: transparent;
        border-right-color: transparent;
        border-bottom-color: transparent;
      }
    }

    .msg {
      min-height: 40px;
      max-width: 80%;
      font-size: 14px;
      line-height: 18px;
      padding: 5px 10px;
      display: flex;
      align-items: center;
      border-radius: 10px;

      &> .image {
        cursor: pointer;
        width: 100%;
        max-width: 300px;
      }
    }

    .readType {
      margin-right: 10px;
      color: #b07474;
      display: flex;
      align-items: center;
    }

    &.chat-left {
      color: #ffffff;
      margin-left: 10px;

      & > div.msg {
        background: $leftColor;
      }

      & > div.box {
        border-right-color: $leftColor;
      }
    }

    &.chat-right {
      flex-direction: row-reverse;
      margin-right: 10px;

      & > div.msg {
        background: $rightColor;
      }

      & > div.box {
        border-left-color: $rightColor;
      }
    }
  }
}
</style>
